home *** CD-ROM | disk | FTP | other *** search
- // =================================================================================
- // SliderCDEF1.3.0.c ©1993-1994 Harold Ekstrom. All rights reserved.
- // =================================================================================
- // SliderCDEF.h
-
- #include <Picker.h>
- #include <TextUtils.h>
-
- #include "SliderCDEF.h"
-
- // ---------------------------------------------------------------------------------
- // • main for SliderCDEF
- // ---------------------------------------------------------------------------------
- // The entry point to the control definition. This function is called directly
- // by the control manager for all control actions.
-
- pascal long
- main( short inVarCode, ControlHandle inControl, short inMessage, long inParam )
- {
- // Initialize the result to zero.
- long theResult = 0L;
-
- // Handle the message.
- switch( inMessage ) {
-
- case drawCntl:
- // According to tech note TB 31, the high word of
- // inParam may contain undefined data.
- inParam = LoWord( inParam );
-
- // If the control is visible, draw it.
- if ( (**inControl).contrlVis ) {
- SliderDrawControl( inVarCode, inControl, inParam );
- }
- break;
-
- case testCntl:
- theResult = SliderTestControl( inVarCode, inControl, inParam );
- break;
-
- case calcCRgns:
- SliderCalcCRgns( inVarCode, inControl, inParam );
- break;
-
- case initCntl:
- // Comment the following line when compiling as stand-alone code.
- // (**inControl).contrlRfCon = (long) SliderCDEF;
-
- SliderInitControl( inVarCode, inControl, inParam );
- break;
-
- case dispCntl:
- SliderDisposeControl( inVarCode, inControl, inParam );
- break;
-
- case posCntl:
- // Not used for a control that does default dragging.
- break;
-
- case thumbCntl:
- // Not used for a control that does default dragging.
- break;
-
- case dragCntl:
- theResult = SliderDragControl( inVarCode, inControl, inParam );
- break;
-
- case autoTrack:
- // According to tech note TB 31, the high word of
- // inParam may contain undefined data.
- inParam = LoWord( inParam );
- SliderAutoTrack( inVarCode, inControl, inParam );
- break;
-
- case calcCntlRgn:
- SliderCalcCntlRgn( inVarCode, inControl, inParam );
- break;
-
- case calcThumbRgn:
- SliderCalcThumbRgn( inVarCode, inControl, inParam );
- break;
- }
-
- return theResult;
- }
-
-
- // ---------------------------------------------------------------------------------
- // • SliderDrawControl
- // ---------------------------------------------------------------------------------
- // The main drawing routine for the CDEF. Called in respone
- // to the drawCtrl message if the control is visible.
- // Saves port characteristics, determines if the slider needs to
- // be redrawn in its offscreen pixmap, redraws if necessary,
- // copies the control to the window, and restores the port.
-
- void
- SliderDrawControl( short inVarCode, ControlHandle inControl, long inParam )
- {
- SliderDataHnd theSliderData;
- GrafPtr theSavedPort;
- PenState theSavedPenState;
- RGBColor theForeColor, theBackColor;
- RgnHandle theSavedClip;
- DeviceLoopDrawingUPP theDeviceLoopUPP;
- GWorldFlags theGWFlags;
- short theCurrDepth;
- short thePosition;
- RgnHandle theVisRgn;
-
- // Check the slider's control handle.
- if ( inControl == nil ) return;
-
- // Get the slider's data.
- theSliderData = GetSliderData( inControl );
- if ( theSliderData == nil ) return;
-
- // Save the pen state.
- GetPenState( &theSavedPenState );
-
- // Save the foreground and background colors.
- GetForeColor( &theForeColor );
- GetBackColor( &theBackColor );
-
- // Save the current clip region.
- theSavedClip = NewRgn();
- GetClip( theSavedClip );
-
- // Save the current port.
- GetPort( &theSavedPort );
-
- // Calculate the new thumb position and put it in the slider's data.
- thePosition = CalcPosition( inControl );
- (**theSliderData).sdPosition = thePosition;
-
- // Find out if the depth has changed and update the offscreen
- // graphics worlds if necessary.
- theCurrDepth = GetDepth( inControl );
- if ( (**theSliderData).sdDepth != theCurrDepth ) {
-
- // Lock the slider data.
- HLock( (Handle) theSliderData );
-
- // Update the track offscreen graphics world.
- theGWFlags = UpdateGWorld( &(**theSliderData).sdTrack, theCurrDepth,
- &(**theSliderData).sdTrack->portRect, 0, nil, 0 );
-
- // Update the thumb offscreen graphics world.
- theGWFlags = UpdateGWorld( &(**theSliderData).sdThumb, theCurrDepth,
- &(**theSliderData).sdThumb->portRect, 0, nil, 0 );
-
- // Keep track of the current depth.
- (**theSliderData).sdDepth = theCurrDepth;
-
- // Unlock the slider data.
- HUnlock( (Handle) theSliderData );
-
- }
-
- // Draw the control's track and the control's indicator
- // in the offscreen graphics world.
- theDeviceLoopUPP = NewDeviceLoopDrawingProc( SliderDrawParts );
- theVisRgn = ((GrafPtr)(**inControl).contrlOwner)->visRgn;
- DeviceLoop( theVisRgn, theDeviceLoopUPP, (long) inControl, 0 );
- DisposeRoutineDescriptor( theDeviceLoopUPP );
-
- // Draw the control. Copy it from the offscreen pixmap to the window.
- theDeviceLoopUPP = NewDeviceLoopDrawingProc( SliderBlitControl );
- theVisRgn = ((GrafPtr)(**inControl).contrlOwner)->visRgn;
- DeviceLoop( theVisRgn, theDeviceLoopUPP, (long) inControl, 0 );
- DisposeRoutineDescriptor( theDeviceLoopUPP );
-
- // Draw the control's value if needed.
- if ( ( (**theSliderData).sdVarCode & kValueAboveCtl ) ) {
-
- // Restore the foreground and background colors.
- // Drawing the value requires correct window colors.
- RGBForeColor( &theForeColor );
- RGBBackColor( &theBackColor );
-
- // Draw the control value.
- theDeviceLoopUPP = NewDeviceLoopDrawingProc( SliderDrawValue );
- theVisRgn = ((GrafPtr)(**inControl).contrlOwner)->visRgn;
- DeviceLoop( theVisRgn, theDeviceLoopUPP, (long) inControl, 0 );
- DisposeRoutineDescriptor( theDeviceLoopUPP );
-
- }
-
- // Restore the port.
- SetPort( theSavedPort );
-
- // Restore the clipping region.
- SetClip( theSavedClip );
- DisposeRgn( theSavedClip );
-
- // Restore the foreground and background colors.
- RGBForeColor( &theForeColor );
- RGBBackColor( &theBackColor );
-
- // Restore the pen state.
- SetPenState( &theSavedPenState );
- }
-
-
- // ---------------------------------------------------------------------------------
- // • SliderBlitControl
- // ---------------------------------------------------------------------------------
- // Copy the slider to the window. Only done in response to the drawCntl message.
- // The control dragging function performs it's own drawing.
-
- pascal void
- SliderBlitControl( short inDepth, short inDeviceFlags,
- GDHandle inTargetDevice, long inUserData )
- {
- ControlHandle theControl;
- SliderDataHnd theSliderData;
- WindowPtr theWindow;
- Rect theRect;
-
- // Get the slider's control handle.
- theControl = (ControlHandle) inUserData;
- if ( theControl == nil ) return;
-
- // Get the slider's data.
- theSliderData = GetSliderData( theControl );
- if ( theSliderData == nil ) return;
-
- // Lock the slider data.
- HLock( (Handle) theSliderData );
-
- // Get the control's parent window.
- theWindow = (**theControl).contrlOwner;
- SetPort( theWindow );
-
- // Set the foreground and background colors for CopyBits.
- ForeColor( blackColor );
- BackColor( whiteColor );
-
- // Calculate the track rect.
- CalcTrackRect( theControl, &theRect );
-
- // Copy the track to the window.
- CopyBits( &((GrafPtr)(**theSliderData).sdTrack)->portBits,
- &((GrafPtr)theWindow)->portBits,
- &(**theSliderData).sdTrackBounds, &theRect, srcCopy, nil );
-
- // Calculate the thumb rect.
- CalcThumbRect( theControl, &theRect );
-
- // Copy the thumb to the window.
- CopyBits( &((GrafPtr)(**theSliderData).sdThumb)->portBits,
- &((GrafPtr)theWindow)->portBits,
- &(**theSliderData).sdThumbBounds, &theRect, srcCopy, nil );
-
- // Unlock the slider data.
- HUnlock( (Handle) theSliderData );
- }
-
-
- // ---------------------------------------------------------------------------------
- // • SliderDrawParts
- // ---------------------------------------------------------------------------------
- // Draw each of the slider's parts into each of the slider's GWorlds.
-
- pascal void
- SliderDrawParts( short inDepth, short inDeviceFlags,
- GDHandle inTargetDevice, long inUserData )
- {
- ControlHandle theControl;
- SliderDataHnd theSliderData;
- GWorldPtr theCurrPort;
- GDHandle theCurrDev;
-
- // Get the slider's control handle.
- theControl = (ControlHandle) inUserData;
- if ( theControl == nil ) return;
-
- // Get the slider's data.
- theSliderData = GetSliderData( theControl );
- if ( theSliderData == nil ) return;
-
- // Get the current port and device.
- GetGWorld( &theCurrPort, &theCurrDev );
-
- // Lock the slider data.
- HLock( (Handle) theSliderData );
-
- // Set the port to the track offscreen graphics world.
- SetGWorld ( (**theSliderData).sdTrack, nil );
-
- // Lock the track's pixels.
- LockPixels( GetGWorldPixMap( (**theSliderData).sdTrack ) );
-
- // Draw the track. Use our own depth.
- SliderDrawTrack( (**theSliderData).sdDepth, theControl );
-
- // Unlock the track's pixels.
- UnlockPixels( GetGWorldPixMap( (**theSliderData).sdTrack ) );
-
- // Set the port to the thumb offscreen graphics world.
- SetGWorld ( (**theSliderData).sdThumb, nil );
-
- // Lock the thumb's pixels.
- LockPixels( GetGWorldPixMap( (**theSliderData).sdThumb ) );
-
- // Draw the thumb. Use our own depth.
- SliderDrawThumb( (**theSliderData).sdDepth, theControl );
-
- // Unlock the track's pixels.
- UnlockPixels( GetGWorldPixMap( (**theSliderData).sdThumb ) );
-
- // Done drawing, now reset port.
- SetGWorld ( theCurrPort, theCurrDev );
- }
-
-
- // ---------------------------------------------------------------------------------
- // • SliderDrawTrack
- // ---------------------------------------------------------------------------------
- // Draws the track for the slider.
-
- void
- SliderDrawTrack( short inDepth, ControlHandle inControl )
- {
- SliderDataHnd theSliderData;
- Rect theTrackBounds;
- Boolean isActive;
- Pattern theGrayPat;
- AuxCtlHandle theAuxHndl;
- CCTabHandle theAuxCTable;
- RGBColor theColor;
-
- // Get out now if theControl is nil.
- if ( inControl == nil ) return;
-
- // Get the slider's data.
- theSliderData = GetSliderData( inControl );
- if ( theSliderData == nil ) return;
-
- // Get a copy of the track bounds (GWorld coordinates).
- theTrackBounds = (**theSliderData).sdTrackBounds;
-
- // Find out if the slider is active.
- isActive = IsSliderActive( inControl );
-
- // Normalize the pen.
- PenNormal();
-
- switch ( inDepth ) {
-
- case 1:
- case 2:
- ForeColor( blackColor );
- BackColor( whiteColor );
-
- // Erase the track.
- EraseRect( &theTrackBounds );
-
- // Draw in gray pattern if disabled.
- if ( !isActive ) {
- GetIndPattern( &theGrayPat, sysPatListID, 4 );
- PenPat( &theGrayPat );
- }
-
- // Frame the track.
- FrameRect( &theTrackBounds );
- break;
-
- default:
- // Get the aux control record.
- GetAuxiliaryControlRecord( inControl, &theAuxHndl );
-
- // Get the color table.
- theAuxCTable = (**theAuxHndl).acCTable;
-
- // Lock the handle to the slider's data.
- HLock( (Handle) theSliderData );
-
- // Find out the slider's activation state and draw accordingly.
- if ( !isActive ) {
-
- // Fill in the track.
- theColor.red = theColor.green = theColor.blue = 0xCCCC;
- RGBForeColor( &theColor );
- PaintRect( &theTrackBounds );
-
- // Frame the track.
- theColor.red = theColor.green = theColor.blue = 0x8888;
- RGBForeColor( &theColor );
- FrameRect( &theTrackBounds );
-
- } else {
-
- // Fill in the track.
- theColor = ((**theAuxCTable).ctTable[13]).rgb;
- RGBForeColor( &theColor );
- PaintRect( &theTrackBounds );
-
- // Frame the track.
- theColor = ((**theAuxCTable).ctTable[0]).rgb;
- RGBForeColor( &theColor );
- FrameRect( &theTrackBounds );
-
- // Outline the bottom and right sides of the track.
- theColor = ((**theAuxCTable).ctTable[9]).rgb;
- RGBForeColor( &theColor );
- theTrackBounds.bottom -= 1;
- theTrackBounds.right -= 1;
- MoveTo( theTrackBounds.left+1, theTrackBounds.bottom-1 );
- LineTo( theTrackBounds.right-1, theTrackBounds.bottom-1 );
- LineTo( theTrackBounds.right-1, theTrackBounds.top+1 );
- }
-
- // Unlock the slider's data.
- HUnlock( (Handle) theSliderData );
- break;
- }
-
- if ( ( (**theSliderData).sdVarCode & kTitleInTrack ) && IsHorizontal( inControl ) ) {
-
- // Draw the title inside the slider track for this
- // variation only if the control is horizontal.
-
- // Check for a blank title.
- if ( (**inControl).contrlTitle[0] != 0 ) {
-
- FontInfo theFInfo;
- short theFontHeight;
-
- // Set the text traits.
- TextFont( kStdFont );
- TextSize( kStdFontSize );
- TextFace( kStdFontFace );
-
- // Get the font info.
- GetFontInfo( &theFInfo );
-
- // Calculate the font height.
- theFontHeight = theFInfo.ascent + theFInfo.descent;
-
- // Get a fresh copy of the track bounds.
- theTrackBounds = (**theSliderData).sdTrackBounds;
-
- // Check for a track shorter than the standard font height.
- if ( theTrackBounds.bottom - theTrackBounds.top >= theFontHeight ) {
-
- Str255 theString;
- short theTrackWidth;
-
- // Copy the slider title.
- BlockMoveData( (**inControl).contrlTitle, theString,
- (**inControl).contrlTitle[0] + 1 );
- // Calculate the track width.
- theTrackWidth = theTrackBounds.right - theTrackBounds.left;
-
- // Trunctate the string if needed.
- if ( TruncString( theTrackWidth, theString, truncEnd ) >= 0 ) {
-
- short theHPos;
- short theVPos;
-
- // Calculate the horizontal position for the text.
- theHPos = theTrackBounds.left +
- ( theTrackWidth - StringWidth( theString ) ) / 2;
-
- // Calculate the vertical position for the text.
- theVPos = theTrackBounds.bottom - ( ( theTrackBounds.bottom -
- theTrackBounds.top ) - theFontHeight ) / 2 - theFInfo.descent - 1;
-
- // Move to the text position.
- MoveTo( theHPos, theVPos );
-
- if ( inDepth >= 8 ) {
- if ( !isActive ) {
-
- // Use a dark gray color.
- theColor.red = theColor.green = theColor.blue = 0x8888;
- RGBForeColor( &theColor );
-
- } else {
-
- // Use black.
- ForeColor( blackColor );
-
- }
- }
-
- // Draw the string.
- DrawString( theString );
- }
- }
- }
- }
- }
-
-
- // ---------------------------------------------------------------------------------
- // • SliderDrawThumb
- // ---------------------------------------------------------------------------------
- // Draws the thumb for the slider.
-
- void
- SliderDrawThumb( short inDepth, ControlHandle inControl )
- {
- SliderDataHnd theSliderData;
- Rect theThumbBounds;
- short theThumbCenter;
- short theThumbWidth;
- Boolean isHorizontal;
- Pattern theGrayPat;
- AuxCtlHandle theAuxHndl;
- CCTabHandle theAuxCTable;
- RGBColor theColor;
- RGBColor theLightColor;
- RGBColor theDarkColor;
-
- // Get out now if theControl is nil.
- if ( inControl == nil ) return;
-
- // Get the slider's data.
- theSliderData = GetSliderData( inControl );
- if ( theSliderData == nil ) return;
-
- // Get a copy of the thumb bounds (GWorld coordinates).
- theThumbBounds = (**theSliderData).sdThumbBounds;
-
- // Find out if the slider is horizontal or vertical.
- isHorizontal = IsHorizontal( inControl );
-
- // Calculate the thumb width and center.
- if ( isHorizontal ) {
- theThumbWidth = (theThumbBounds.right - theThumbBounds.left);
- theThumbCenter = theThumbBounds.left + theThumbWidth/2 - 1;
- } else {
- theThumbWidth = (theThumbBounds.bottom - theThumbBounds.top);
- theThumbCenter = theThumbBounds.top + theThumbWidth/2 - 1;
- }
-
- // Normalize the pen.
- PenNormal();
-
- switch ( inDepth ) {
-
- case 1:
- case 2:
- // Drawing in B&W, just frame the track.
- ForeColor( blackColor );
- BackColor( whiteColor );
-
- // Erase the track.
- EraseRect( &theThumbBounds );
-
- if ( !IsSliderActive( inControl ) ) {
-
- // Draw in gray pattern.
- GetIndPattern( &theGrayPat, sysPatListID, 4 );
- PenPat( &theGrayPat );
-
- }
-
- // Frame the thumb.
- FrameRect( &theThumbBounds );
-
- // Draw the thumb centerline.
- if ( isHorizontal ) {
- MoveTo( theThumbCenter, theThumbBounds.bottom-1 );
- LineTo( theThumbCenter, theThumbBounds.top );
- } else {
- MoveTo( theThumbBounds.left+1, theThumbCenter );
- LineTo( theThumbBounds.right, theThumbCenter );
- }
- break;
-
- default:
- // Get the aux control record.
- GetAuxiliaryControlRecord( inControl, &theAuxHndl );
-
- // Get the color table.
- theAuxCTable = (**theAuxHndl).acCTable;
-
- // Lock the handle to the slider's data.
- HLock( (Handle) theSliderData );
-
- // Find out the slider's state and draw accordingly.
- if ( !IsSliderActive( inControl ) ) {
-
- // Fill in the thumb rect.
- theColor.red = theColor.green = theColor.blue = 0xCCCC;
- RGBForeColor( &theColor );
- PaintRect( &theThumbBounds );
-
- // Frame is with the disabled color.
- theColor.red = theColor.green = theColor.blue = 0x8888;
- RGBForeColor( &theColor );
- FrameRect( &theThumbBounds );
-
- // Draw the thumb center line.
- if ( isHorizontal ) {
- MoveTo( theThumbCenter, theThumbBounds.bottom-1 );
- LineTo( theThumbCenter, theThumbBounds.top );
- } else {
- MoveTo( theThumbBounds.left, theThumbCenter );
- LineTo( theThumbBounds.right, theThumbCenter );
- }
-
-
- } else {
-
- // Get the light and dark highlight colors from the control color table.
- theLightColor = ((**theAuxCTable).ctTable[13]).rgb;
- theDarkColor = ((**theAuxCTable).ctTable[14]).rgb;
-
- // Find a good dark color based on the highlight colors.
- MixColors( &theLightColor, &theDarkColor, kThumbShade, &theColor );
-
- // Fill in the thumb rect.
- RGBForeColor( &theColor );
- PaintRect( &theThumbBounds );
-
- // Use the dark color to frame the thumb.
- RGBForeColor( &theDarkColor );
-
- if ( isHorizontal ) {
-
- // Outline the left, bottom, right, and center line of the thumb.
- MoveTo( theThumbBounds.left, theThumbBounds.top );
- LineTo( theThumbBounds.left, theThumbBounds.bottom-1 );
- LineTo( theThumbBounds.right-1, theThumbBounds.bottom-1 );
- LineTo( theThumbBounds.right-1, theThumbBounds.top );
- MoveTo( theThumbCenter, theThumbBounds.bottom-1 );
- LineTo( theThumbCenter, theThumbBounds.top );
-
- } else {
-
- // Outline the top, bottom, right, and center line of the thumb.
- MoveTo( theThumbBounds.left, theThumbBounds.top );
- LineTo( theThumbBounds.right-1, theThumbBounds.top );
- LineTo( theThumbBounds.right-1, theThumbBounds.bottom-1 );
- LineTo( theThumbBounds.left, theThumbBounds.bottom-1 );
- MoveTo( theThumbBounds.left, theThumbCenter );
- LineTo( theThumbBounds.right, theThumbCenter );
-
- }
-
- // Add some highlights.
- theColor = ((**theAuxCTable).ctTable[9]).rgb;
- RGBForeColor( &theColor );
- if ( isHorizontal ) {
-
- MoveTo( theThumbBounds.left+1, theThumbBounds.bottom-2 );
- LineTo( theThumbBounds.left+1, theThumbBounds.top );
- LineTo( theThumbCenter-1, theThumbBounds.top );
- MoveTo( theThumbCenter+1, theThumbBounds.bottom-2 );
- LineTo( theThumbCenter+1, theThumbBounds.top );
- LineTo( theThumbBounds.right-2, theThumbBounds.top );
-
- } else {
-
- MoveTo( theThumbBounds.left, theThumbBounds.bottom-2 );
- LineTo( theThumbBounds.left, theThumbCenter+1 );
- LineTo( theThumbBounds.right-2, theThumbCenter+1 );
- MoveTo( theThumbBounds.left, theThumbCenter-1 );
- LineTo( theThumbBounds.left, theThumbBounds.top+1 );
- LineTo( theThumbBounds.right-2, theThumbBounds.top+1 );
-
- }
- }
-
- // Unlock the slider's data.
- HUnlock( (Handle) theSliderData );
- break;
- }
- }
-
-
- // ---------------------------------------------------------------------------------
- // • SliderDrawValue
- // ---------------------------------------------------------------------------------
- // Draws the control value centered over the thumb just above or to the left of the
- // slider's rectangle. Note: drawing occurs outside the control's rectangle!
-
- pascal void
- SliderDrawValue( short inDepth, short inDeviceFlags,
- GDHandle inTargetDevice, long inUserData )
- {
- ControlHandle theControl;
- SliderDataHnd theSliderData;
- WindowPtr theWindow;
- long theValue;
- short theStringWidth;
- short theStringHalfWidth;
- short theHPos;
- short theVPos;
- short theThumbCenter;
- Rect wThumbRect;
- Rect theTextRect;
- FontInfo theFInfo;
- Str255 theValueString;
- short savedTextFont;
- short savedTextStyle;
- short savedTextSize;
- RgnHandle savedClip;
- RgnHandle newClip;
- Pattern grayPattern;
- AuxCtlHandle acHndl;
- CCTabHandle acCTable;
- RGBColor theColor;
-
- // Get the slider's control handle.
- theControl = (ControlHandle) inUserData;
- if ( theControl == nil ) return;
-
- // Get the slider's data.
- theSliderData = GetSliderData( theControl );
- if ( theSliderData == nil ) return;
-
- // Get the control's parent window.
- theWindow = (WindowPtr) (**theControl).contrlOwner;
- SetPort( theWindow );
-
- // Save the current clip region.
- savedClip = NewRgn();
- GetClip( savedClip );
-
- // Save the font characteristics.
- savedTextFont = ((GrafPtr)theWindow)->txFont;
- savedTextStyle = ((GrafPtr)theWindow)->txFace;
- savedTextSize = ((GrafPtr)theWindow)->txSize;
-
- // Get the control's value.
- theValue = (long) (**theControl).contrlValue;
-
- // Turn the value into a string.
- NumToString( theValue, theValueString );
-
- if ( (**theSliderData).sdVarCode & kTitleAfterValue ) {
-
- // Concatenate the control title to the value string.
- ConcatPStr( theValueString, (**theControl).contrlTitle );
-
- }
-
- if ( (**theSliderData).sdVarCode & kTitleBeforeValue ) {
-
- // Prefix the control title to the value string.
- PrefixPStr( theValueString, (**theControl).contrlTitle );
-
- }
-
- // Set the text attributes.
- TextFont( kStdFont );
- TextSize( kStdFontSize );
- TextFace( kStdFontFace );
-
- // Calculate the string width and half width.
- theStringWidth = StringWidth( theValueString );
- theStringHalfWidth = theStringWidth / 2;
-
- // Get the font information structure.
- GetFontInfo( &theFInfo );
-
- // Get the thumb rectangle (window coordinates).
- CalcThumbRect( theControl, &wThumbRect );
-
- // Set the text rectangle to the control's rectangle.
- theTextRect = (**theControl).contrlRect;
-
- if ( IsHorizontal( theControl ) ) {
-
- // Calculate the thumb center.
- theThumbCenter = wThumbRect.left+(wThumbRect.right-wThumbRect.left)/2;
-
- // Calculate the horizontal position of the text.
- theHPos = theThumbCenter - theStringHalfWidth;
-
- // Calculate the vertical position of the text.
- theVPos = (**theControl).contrlRect.top - 2;
-
- // Calculate the top of the text rectangle.
- theTextRect.bottom = (**theControl).contrlRect.top;
- theTextRect.top = theTextRect.bottom - ( theFInfo.ascent + theFInfo.descent );
-
- } else {
-
- // Calculate the thumb center.
- theThumbCenter = wThumbRect.top+(wThumbRect.bottom-wThumbRect.top)/2;
-
- // Calculate the horizontal position of the text.
- theHPos = (**theControl).contrlRect.right + 2;
-
- // Calculate the vertical position of the text.
- theVPos = theThumbCenter + ( theFInfo.ascent+theFInfo.descent )/2;
-
- // Calculate the left side of the text rectangle.
- theTextRect.left = theHPos;
-
- // Calculate the right side of the text rectangle plus an extra character.
- theTextRect.right = theTextRect.left + theStringWidth + theFInfo.widMax;
-
- }
-
- // Clip to this rectangle.
- newClip = NewRgn();
- RectRgn( newClip, &theTextRect );
- SectRgn( newClip, savedClip, newClip );
-
- if ( !EmptyRgn( newClip ) ) {
-
- // Set the new clip region.
- SetClip( newClip );
-
- // Erase the text rect.
- EraseRect( &theTextRect );
-
- // Find out the slider's activation state set the text color.
- if ( inDepth == 1 ) {
-
- ForeColor( blackColor );
- BackColor( whiteColor );
-
- } else {
-
- // Get the aux control record.
- GetAuxiliaryControlRecord( theControl, &acHndl );
-
- // Get the color table.
- acCTable = (**acHndl).acCTable;
-
- if ( IsSliderActive( theControl ) ) {
- theColor = ((**acCTable).ctTable[2]).rgb;
- RGBForeColor( &theColor );
- } else {
- theColor.red = theColor.green = theColor.blue = 0x8888;
- RGBForeColor( &theColor );
- }
- }
-
- // Move to the text position.
- MoveTo( theHPos, theVPos );
-
- // Draw the text.
- DrawString( theValueString );
-
- // To dim out a black and white slider's value, paint a
- // gray patterned rectangle over it.
- if ( inDepth == 1 && !IsSliderActive( theControl ) ) {
- PenNormal();
- GetIndPattern( &grayPattern, sysPatListID, 4 );
- PenPat( &grayPattern );
- PenMode( patBic );
- PaintRect( &theTextRect );
- }
- }
-
- // Restore the clipping region.
- SetClip( savedClip );
- DisposeRgn( savedClip );
- DisposeRgn( newClip );
-
- // Restore the font characteristics.
- ((GrafPtr)theWindow)->txFont = savedTextFont;
- ((GrafPtr)theWindow)->txFace = savedTextStyle;
- ((GrafPtr)theWindow)->txSize = savedTextSize;
- }
-
-
- // ---------------------------------------------------------------------------------
- // • SliderTestControl
- // ---------------------------------------------------------------------------------
- // Finds the part, if any, that the point is in.
-
- long
- SliderTestControl( short inVarCode, ControlHandle inControl, long inParam )
- {
- Rect theRect;
- Point thePt;
- long theResult = 0L;
-
- // The test point is contained in inParam.
- thePt.v = HiWord( inParam );
- thePt.h = LoWord( inParam );
-
- // Get the drag rect.
- CalcDragRect( inControl, &theRect );
-
- // Test if the point is in the drag.
- if ( PtInRect( thePt, &theRect ) ) {
-
- // Get the thumb rectangle (window coordinates).
- CalcThumbRect( inControl, &theRect );
-
- if ( PtInRect( thePt, &theRect ) ) {
-
- // Point was in the thumb.
- theResult = inThumb;
-
- } else {
-
- // Point was in page up or down area.
- if ( IsHorizontal( inControl ) ) {
- theResult = ( thePt.h < theRect.left ) ? inPageDown : inPageUp;
- } else {
- theResult = ( thePt.v < theRect.top ) ? inPageDown : inPageUp;
- }
-
- }
- }
-
- return theResult;
- }
-
-
- #define kCalcCRgnsMask (0x7FFFFFFFL)
-
- // ---------------------------------------------------------------------------------
- // • SliderCalcCRgns
- // ---------------------------------------------------------------------------------
- // Calculates either the entire control's region which is just the control rectangle for
- // the slider control or the indicator's region, depending on the high bit of inParam.
- // Note: the calcCRgns message is only sent by the control manager when running
- // in 24-bit mode. Under 32-bit mode, two messages are sent: one for the entire
- // control (calcCntlRgn), and one for the indicator (calcThumbRgn).
-
- void
- SliderCalcCRgns( short inVarCode, ControlHandle inControl, long inParam )
- {
- // Get the control rectangle.
- Rect theRect = (**inControl).contrlRect;
-
- // Since we're in 24-bit mode, make sure the high
- // order bits of inParam are clear.
- inParam &= kCalcCRgnsMask;
-
- // Put the control region into inParam.
- RectRgn( (RgnHandle) inParam, &theRect );
- }
-
- #undef kCalcCRgnsMask
-
-
- // ---------------------------------------------------------------------------------
- // • SliderInitControl
- // ---------------------------------------------------------------------------------
- // Performs initialization for the control definition function such as putting some
- // data in the contrlData or contrlRfCon fields or setting the contrlAction field to
- // (Ptr) -1L so that the control manager will call the CDEF with the autoTrack
- // message.
-
- void
- SliderInitControl( short inVarCode, ControlHandle inControl, long inParam )
- {
- SliderDataHnd theSliderData;
-
- // Indicate that auto-tracking is requested.
- (**inControl).contrlAction = (ControlActionUPP) -1L;
-
- // Allocate the slider's data and place a handle to the data structure
- // in the contrlData field.
- theSliderData = (SliderDataHnd) NewHandleClear( sizeof( SliderData ) );
-
- if ( theSliderData != nil ) {
-
- OSErr err;
- short thePosition;
-
- // Stuff the slider data handle in the control's contrlData field.
- (**inControl).contrlData = (Handle) theSliderData;
-
- // Store the current depth.
- (**theSliderData).sdDepth = GetDepth( inControl );
-
- // Store the variation code.
- (**theSliderData).sdVarCode = inVarCode;
-
- // Calculate the new thumb rect and put it in the slider's data.
- thePosition = CalcPosition( inControl );
- (**theSliderData).sdPosition = thePosition;
-
- // Create the offscreen graphics world for the slider.
- err = SliderCreateOffscreenWorld( inControl );
-
- if ( err != noErr ) {
-
- // Initialization failed!
- DisposeHandle( (Handle) theSliderData );
- (**inControl).contrlData = nil;
-
- }
- }
- }
-
-
- // ---------------------------------------------------------------------------------
- // • SliderCreateOffscreenWorld
- // ---------------------------------------------------------------------------------
- // Create the offscreen graphics world for the slider control.
-
- OSErr
- SliderCreateOffscreenWorld( ControlHandle inControl )
- {
- OSErr err = noErr;
- SliderDataHnd theSliderData;
-
- // Get the handle to the slider's data and lock it.
- theSliderData = GetSliderData( inControl );
-
- if ( theSliderData != nil ) {
-
- CGrafPtr theSavePort;
- GDHandle theSaveDevice;
- Rect theRect;
-
- // Save the current port and device.
- GetGWorld( &theSavePort, &theSaveDevice );
-
- // Lock the slider data.
- HLock( (Handle) theSliderData );
-
- // Calculate the track rect.
- CalcTrackRect( inControl, &theRect );
-
- // Offset to (0,0) and store in the slider data.
- SetRect( &(**theSliderData).sdTrackBounds, 0, 0, theRect.right-theRect.left,
- theRect.bottom-theRect.top );
-
- // Create the track gworld.
- err = NewGWorld( &(**theSliderData).sdTrack, 0,
- &(**theSliderData).sdTrackBounds, nil, nil, 0 );
- if ( err != noErr || (**theSliderData).sdTrack == nil ) return err;
-
- // Erase the new gworld.
- SetGWorld( (**theSliderData).sdTrack, nil );
- LockPixels( GetGWorldPixMap( (**theSliderData).sdTrack ) );
- EraseRect( &(**theSliderData).sdTrackBounds );
- UnlockPixels( GetGWorldPixMap( (**theSliderData).sdTrack ) );
-
- // Calculate the thumb rect.
- CalcThumbRect( inControl, &theRect );
-
- // Offset to (0,0) and store in the slider data.
- SetRect( &(**theSliderData).sdThumbBounds, 0, 0, theRect.right-theRect.left,
- theRect.bottom-theRect.top );
-
- // Create the thumb gworld.
- err = NewGWorld( &(**theSliderData).sdThumb, 0,
- &(**theSliderData).sdThumbBounds, nil, nil, 0 );
- if ( err != noErr || (**theSliderData).sdThumb == nil ) {
- DisposeGWorld( (**theSliderData).sdTrack );
- return err;
- }
-
- // Erase the new gworld.
- SetGWorld( (**theSliderData).sdThumb, nil );
- LockPixels( GetGWorldPixMap( (**theSliderData).sdThumb ) );
- EraseRect( &(**theSliderData).sdThumbBounds );
- UnlockPixels( GetGWorldPixMap( (**theSliderData).sdThumb ) );
-
- // Unlock the slider's data.
- HUnlock( (Handle) theSliderData );
-
- // Restore the current port and device.
- SetGWorld( theSavePort, theSaveDevice );
-
- }
-
- return err;
- }
-
-
- // ---------------------------------------------------------------------------------
- // • SliderDisposeControl
- // ---------------------------------------------------------------------------------
- // This routine is called in response to the dispCntl message
- // so that the CDEF may do additional disposal actions.
-
- void
- SliderDisposeControl( short inVarCode, ControlHandle inControl, long inParam )
- {
- if ( inControl != nil ) {
-
- // Get the handle to the slider's data.
- SliderDataHnd theSliderData = GetSliderData( inControl );
-
- if ( theSliderData != nil ) {
-
- // Lock the slider data.
- HLock( (Handle) theSliderData );
-
- // Destroy the track offscreen graphics world.
- if ( (**theSliderData).sdTrack != nil ) {
- DisposeGWorld( (**theSliderData).sdTrack );
- }
-
- // Destroy the thumb offscreen graphics world.
- if ( (**theSliderData).sdThumb != nil ) {
- DisposeGWorld( (**theSliderData).sdThumb );
- }
-
- // Dispose of the slider's private data.
- DisposeHandle( (Handle) theSliderData );
- (**inControl).contrlData = nil;
- }
- }
- }
-
-
- // ---------------------------------------------------------------------------------
- // • SliderDragControl
- // ---------------------------------------------------------------------------------
- // Perform custom dragging of the control thumb.
-
- long
- SliderDragControl( short inVarCode, ControlHandle inControl, long inParam )
- {
- SliderDataHnd theSliderData;
- WindowPtr theWindow;
- RgnHandle theVisRgn;
- RGBColor theForeColor;
- RGBColor theBackColor;
- DeviceLoopDrawingUPP theDeviceLoopUPP;
- Point theStartPt;
- Point theMousePt;
- short theRange;
- Rect theDragRect;
- Rect theThumbRect;
- Rect theWTrackPiece;
- Rect theGWTrackPiece;
- Rect theSlopRect;
- short theLimit = 0;
- short theDragLimit;
- short theOffset;
- short thePosition;
- short theOriginalPosition;
- short thePinPt;
- short theSetPt;
- short theValue;
- Boolean isHorizontal;
-
- // Get out now if theControl is nil.
- if ( inControl == nil ) return 0L;
-
- // Get the slider's data.
- theSliderData = GetSliderData( inControl );
- if ( theSliderData == nil ) return 0L;
-
- // Get the starting mouse location.
- GetMouse( &theStartPt );
-
- // Calculate the thumb rect.
- CalcThumbRect( inControl, &theThumbRect );
-
- // Check if the start point is in the thumb.
- if ( !PtInRect( theStartPt, &theThumbRect ) ) return 0L;
-
- // Lock the slider's data.
- HLock( (Handle) theSliderData );
-
- // Calculate the drag rect.
- CalcDragRect( inControl, &theDragRect );
-
- // Calculate the slop rect.
- theSlopRect = theDragRect;
- InsetRect( &theSlopRect, -20, -20 );
-
- // Store the original position for redrawing when outside the slop rect.
- theOriginalPosition = (**theSliderData).sdPosition;
-
- // Get the control's parent window.
- theWindow = (WindowPtr) (**inControl).contrlOwner;
- SetPort( theWindow );
-
- // Save the foreground and background colors.
- GetForeColor( &theForeColor );
- GetBackColor( &theBackColor );
-
- // Create the DeviceLoopDrawingUPP for drawing control values.
- theDeviceLoopUPP = NewDeviceLoopDrawingProc( SliderDrawValue );
-
- // Find out if the slider is horizontal or vertical.
- isHorizontal = IsHorizontal( inControl );
-
- // Calcuate the range of allowed control values.
- theRange = (**inControl).contrlMax - (**inControl).contrlMin;
-
- // Calculate dragging parameters.
- if ( isHorizontal ) {
- theDragLimit = (theDragRect.right-theDragRect.left) -
- (theThumbRect.right-theThumbRect.left);
- thePinPt = theThumbRect.right - theStartPt.h;
- } else {
- theDragLimit = (theDragRect.bottom-theDragRect.top) -
- (theThumbRect.bottom-theThumbRect.top);
- thePinPt = theThumbRect.bottom - theStartPt.v;
- }
-
- // Loop while the mouse is down.
- while( StillDown() ) {
-
- // Get the mouse location.
- GetMouse( &theMousePt );
-
- // Calculate the offset.
- if( !isHorizontal ) {
- theOffset = theMousePt.v - theStartPt.v;
- } else {
- theOffset = theMousePt.h - theStartPt.h;
- }
-
- // Check if the control was in a limit.
- if ( theLimit != 0 ) {
-
- // Calculate the thumb rect.
- CalcThumbRect( inControl, &theThumbRect );
-
- // Calculate the present offset of the cursor
- // in the thumb.
- if ( isHorizontal ) {
- theSetPt = theThumbRect.right - theMousePt.h;
- } else {
- theSetPt = theThumbRect.bottom - theMousePt.v;
- }
-
- if ( theLimit > 0 ) {
- // The thumb was in the max limit.
- if ( theSetPt < thePinPt ) {
- // Thumb is still in the limit.
- theOffset = 0;
- } else {
- // No longer in limit.
- theLimit = 0;
- theOffset = thePinPt-theSetPt;
- }
- } else {
- // The thumb was in the min limit.
- if ( theSetPt > thePinPt ) {
- // Thumb is still in the limit.
- theOffset = 0;
- } else {
- // No longer in limit.
- theLimit = 0;
- theOffset = thePinPt-theSetPt;
- }
- }
-
- }
-
- if ( theOffset != 0 ) {
-
- // Calculate the new position.
- thePosition = (**theSliderData).sdPosition + theOffset;
-
- // Calculate the new value.
- theValue = (float) thePosition / (float) theDragLimit * theRange +
- (**inControl).contrlMin;
-
- // Clamp the position inside the drag area,
- // and set the limit flag.
- if ( thePosition > theDragLimit ) {
- thePosition = theDragLimit;
- theLimit = 1;
- } else if ( thePosition < 0 ) {
- thePosition = 0;
- theLimit = -1;
- }
-
- // Clamp the control value.
- if ( theValue > (**inControl).contrlMax ) {
- theValue = (**inControl).contrlMax;
- } else if ( theValue < (**inControl).contrlMin ) {
- theValue = (**inControl).contrlMin;
- }
-
- // Check if the position has actually changed.
- if ( (**theSliderData).sdPosition != thePosition ) {
-
- // Calculate the thumb rect at the old position.
- CalcThumbRect( inControl, &theWTrackPiece );
-
- // Update the position and control value.
- (**theSliderData).sdPosition = thePosition;
- (**inControl).contrlValue = theValue;
-
- // Calculate the thumb rect at the new position.
- CalcThumbRect( inControl, &theThumbRect );
-
- // Calculate the rectangle that will cover the old thumb position.
- if ( isHorizontal ) {
- if ( theThumbRect.left < theWTrackPiece.right &&
- theThumbRect.right > theWTrackPiece.right ) {
- theWTrackPiece.right = theThumbRect.left;
- } else if ( theThumbRect.right > theWTrackPiece.left &&
- theThumbRect.left < theWTrackPiece.left ) {
- theWTrackPiece.left = theThumbRect.right;
- }
- } else {
- if ( theThumbRect.top < theWTrackPiece.bottom &&
- theThumbRect.bottom > theWTrackPiece.bottom ) {
- theWTrackPiece.bottom = theThumbRect.top;
- } else if ( theThumbRect.bottom > theWTrackPiece.top &&
- theThumbRect.top < theWTrackPiece.top ) {
- theWTrackPiece.top = theThumbRect.bottom;
- }
- }
-
- // Transform the rect into gworld coordinates.
- theGWTrackPiece = theWTrackPiece;
- OffsetRect( &theGWTrackPiece, -(**inControl).contrlRect.left,
- -(**inControl).contrlRect.top );
-
- // Set the foreground and background colors for CopyBits.
- ForeColor( blackColor );
- BackColor( whiteColor );
-
- // Copy a piece of the track to the window to cover
- // the previous thumb position.
- CopyBits( &((GrafPtr)(**theSliderData).sdTrack)->portBits,
- &((GrafPtr)theWindow)->portBits,
- &theGWTrackPiece, &theWTrackPiece, srcCopy, nil );
-
- // Copy the thumb to the window.
- CopyBits( &((GrafPtr)(**theSliderData).sdThumb)->portBits,
- &((GrafPtr)theWindow)->portBits,
- &(**theSliderData).sdThumbBounds,
- &theThumbRect, srcCopy, nil );
-
- if ( (**theSliderData).sdVarCode & kValueAboveCtl ) {
-
- // Restore the foreground and background colors.
- RGBForeColor( &theForeColor );
- RGBBackColor( &theBackColor );
-
- // Draw the control value.
- theVisRgn = ((GrafPtr)(**inControl).contrlOwner)->visRgn;
- DeviceLoop( theVisRgn, theDeviceLoopUPP, (long) inControl, 0 );
- }
- }
- }
-
- // Update the start point.
- theStartPt = theMousePt;
- }
-
- // Unlock the slider's data.
- HUnlock( (Handle) theSliderData );
-
- // Dispose of the DeviceLoopDrawingUPP.
- DisposeRoutineDescriptor( theDeviceLoopUPP );
-
- // Restore the foreground and background colors.
- RGBForeColor( &theForeColor );
- RGBBackColor( &theBackColor );
-
- // Return a non-zero value to indicate dragging was handled by the CDEF.
- return 1L;
- }
-
-
- // ---------------------------------------------------------------------------------
- // • SliderAutoTrack
- // ---------------------------------------------------------------------------------
- // Adjust the control's value when clicked in the up or down arrows or the page-up
- // or page-down areas. TrackControl must be called with a value of -1 for the
- // action procedure to receive auto-tracking.
-
- void
- SliderAutoTrack( short inVarCode, ControlHandle inControl, long inParam )
- {
- SliderDataHnd theSliderData;
- short theRange;
- short theValue;
- short theOffset = 0;
- short theNewPosition;
-
- // Get the slider's data.
- theSliderData = GetSliderData( inControl );
- if ( theSliderData == nil ) return;
-
- // Calculate the range of control values.
- theRange = (**inControl).contrlMax - (**inControl).contrlMin;
-
- // Calculate how much too move the control.
- switch ( inParam ) {
- case inUpButton:
- theOffset = 1;
- break;
- case inDownButton:
- theOffset = -1;
- break;
- case inPageUp:
- theOffset = theRange / 10;
- if ( theOffset == 0 ) theOffset = 1;
- break;
- case inPageDown:
- theOffset = - theRange / 10;
- if ( theOffset == 0 ) theOffset = -1;
- break;
- }
-
- if ( theOffset != 0 ) {
-
- // Calculate the new control value.
- theValue = (**inControl).contrlValue;
- theValue += theOffset;
-
- // Make sure the limits are not exceeded.
- if ( theValue < (**inControl).contrlMin ) theValue = (**inControl).contrlMin;
- if ( theValue > (**inControl).contrlMax ) theValue = (**inControl).contrlMax;
-
- // Set the new control value.
- (**inControl).contrlValue = theValue;
-
- // Calculate the new thumb rect and put it in the slider's data.
- theNewPosition = CalcPosition( inControl );
- (**theSliderData).sdPosition = theNewPosition;
-
- // Redraw the control.
- SliderDrawControl( inVarCode, inControl, 0 );
- }
- }
-
-
- // ---------------------------------------------------------------------------------
- // • SliderCalcCntlRgn
- // ---------------------------------------------------------------------------------
- // Calculates the entire control's region which is just the control rectangle for
- // the slider control. Note: the calcCntlRgn message is only sent by the control
- // manager when running in 32-bit mode. Under 24-bit mode the single message
- // calcCRgns is sent for all region calculations.
-
- void
- SliderCalcCntlRgn( short inVarCode, ControlHandle theControl, long inParam )
- {
- // Get the control rect.
- Rect contrlRect = (**theControl).contrlRect;
-
- // Put a handle to the slider's control region into inParam.
- RectRgn( (RgnHandle) inParam, &contrlRect );
- }
-
-
- // ---------------------------------------------------------------------------------
- // • SliderCalcThumbRgn
- // ---------------------------------------------------------------------------------
- // Calculates the entire indicator's region. Note: the calcCntlRgn message is only
- // sent by the control manager when running in 32-bit mode. Under 24-bit mode
- // the single message calcCRgns is sent for all region calculations.
-
- void
- SliderCalcThumbRgn( short inVarCode, ControlHandle inControl, long inParam )
- {
- // Get a handle to the slider's private data.
- SliderDataHnd theSliderData = GetSliderData( inControl );
-
- if ( theSliderData ) {
-
- Rect theThumbRect;
-
- // Get the thumb's rect.
- CalcThumbRect( inControl, &theThumbRect );
-
- // Give the control manager a handle to the thumb's region.
- RectRgn( (RgnHandle) inParam, &theThumbRect );
- }
- }
-
-
- // ---------------------------------------------------------------------------------
- // • CalcPosition
- // ---------------------------------------------------------------------------------
- // Calculate the thumb position.
-
- short
- CalcPosition( ControlHandle inControl )
- {
- Rect theDragRect;
- Rect theThumbRect;
- short theDragLimit = 0;
- short thePosition;
-
- // Calculate the drag rect.
- CalcDragRect( inControl, &theDragRect );
-
- // Calculate the thumb rect. Actually, all that is needed is the
- // thumb size. If the thumb size were variable width, this routine
- // wouldn't need to be modified.
- CalcThumbRect( inControl, &theThumbRect );
-
- // Calculate the drag limit.
- if ( IsHorizontal( inControl ) ) {
- theDragLimit = (theDragRect.right - theDragRect.left) -
- (theThumbRect.right - theThumbRect.left);
- } else {
- theDragLimit = (theDragRect.bottom - theDragRect.top) -
- (theThumbRect.bottom - theThumbRect.top);
- }
-
- // Calculate the thumb position.
- thePosition = theDragLimit *
- (float) ((**inControl).contrlValue-(**inControl).contrlMin) /
- (float) ((**inControl).contrlMax-(**inControl).contrlMin);
- if ( thePosition < 0 ) {
- thePosition = 0;
- } else if ( thePosition > theDragLimit ) {
- thePosition = theDragLimit;
- }
-
- return thePosition;
- }
-
-
- // ---------------------------------------------------------------------------------
- // • CalcDragRect
- // ---------------------------------------------------------------------------------
- // Calculate the drag rectangle. This is the track rectangle minus any highlights.
-
- void
- CalcDragRect( ControlHandle inControl, Rect *outRect )
- {
- // Calculate the track rect.
- CalcTrackRect( inControl, outRect );
-
- // Inset the rect to account for the highlights.
- InsetRect( outRect, 1, 1 );
- }
-
-
- // ---------------------------------------------------------------------------------
- // • CalcTrackRect
- // ---------------------------------------------------------------------------------
- // Calculate the track rectangle.
-
- void
- CalcTrackRect( ControlHandle inControl, Rect *outRect )
- {
- // The track rect is (for now) the same as the control rect.
- // If the control had arrows on the end they would be subtracted
- // from the control rect here.
- *outRect = (**inControl).contrlRect;
- }
-
-
- // ---------------------------------------------------------------------------------
- // • CalcThumbRect
- // ---------------------------------------------------------------------------------
- // Calculate the thumb rectangle based on the track rectangle and the current position.
-
- void
- CalcThumbRect( ControlHandle inControl, Rect *outRect )
- {
- // Get a handle to the slider's private data.
- SliderDataHnd theSliderData = GetSliderData( inControl );
-
- if ( theSliderData != nil ) {
-
- // Calculate the drag rectangle.
- CalcDragRect( inControl, outRect );
-
- if ( IsHorizontal( inControl ) ) {
-
- outRect->left = outRect->left + (**theSliderData).sdPosition;
- outRect->right = outRect->left + kStdThumbSize;
-
- } else {
-
- outRect->top = outRect->top + (**theSliderData).sdPosition;
- outRect->bottom = outRect->top + kStdThumbSize;
-
- }
- }
- }
-
-
- // ---------------------------------------------------------------------------------
- // • IsHorizontal
- // ---------------------------------------------------------------------------------
- // Looks at the control's contrlRect to see whether its width is greater than its height.
- // If true, the control is horizontal, otherwise it's vertical.
-
- Boolean
- IsHorizontal( ControlHandle inControl )
- {
- // Make a local copy of the control's rect.
- Rect theRect = (**inControl).contrlRect;
-
- // Compare the control's width to its height.
- return ( (theRect.right-theRect.left) > (theRect.bottom-theRect.top) ) ? true : false;
- }
-
-
- // ---------------------------------------------------------------------------------
- // • GetDepth
- // ---------------------------------------------------------------------------------
- // Find the depth of the control's port.
-
- short
- GetDepth( ControlHandle theControl )
- {
- if ( ((CGrafPtr)(**theControl).contrlOwner)->portVersion == ((short)0xC000) ) {
- return (**((CGrafPtr)(**theControl).contrlOwner)->portPixMap).pixelSize;
- } else {
- return 1;
- }
- }
-
-
- // ---------------------------------------------------------------------------------
- // • MixColors
- // ---------------------------------------------------------------------------------
- // Mix two rgb colors to a specific shade (0-15).
- // Borrowed from the Infinity Windoid by Troy Gaul.
-
- void
- MixColors( const RGBColor *inLight, const RGBColor *inDark,
- short inShade, RGBColor *outResult )
- {
- // This is necessary because we give shades between light and
- // dark (0% is light), but for colors, $0000 is black and $FFFF
- // is dark.
- inShade = 0x0F - inShade;
-
- // Calculate the mixed color.
- outResult->red = (long) (inLight->red - inDark->red) *
- inShade / 15 + inDark->red;
- outResult->green = (long) (inLight->green - inDark->green) *
- inShade / 15 + inDark->green;
- outResult->blue = (long) (inLight->blue - inDark->blue) *
- inShade / 15 + inDark->blue;
- }
-
-
- // ---------------------------------------------------------------------------------
- // • CopyPStr
- // ---------------------------------------------------------------------------------
- // Copy the source string into the destination string.
-
- StringPtr
- CopyPStr( ConstStr255Param inSourceStr, Str255 outDestStr )
- {
- // Copy the source string into the dest string.
- BlockMoveData( inSourceStr, outDestStr, *inSourceStr + 1 );
-
- return outDestStr;
- }
-
-
- // ---------------------------------------------------------------------------------
- // • ConcatPStr
- // ---------------------------------------------------------------------------------
- // Concatenate the second string to the first string.
-
- StringPtr
- ConcatPStr( Str255 ioFirstStr, ConstStr255Param inSecondStr )
- {
- // Limit the combined string to 255 chars.
- Size theNumChars = *inSecondStr;
- if ( *ioFirstStr + theNumChars > 255 ) {
- theNumChars = 255 - *ioFirstStr;
- }
-
- // Copy second string to end of first string
- BlockMoveData( inSecondStr + 1, ioFirstStr + *ioFirstStr + 1, theNumChars );
-
- // Set length of combined string
- *ioFirstStr += theNumChars;
-
- return ioFirstStr;
- }
-
-
- // ---------------------------------------------------------------------------------
- // • PrefixPStr
- // ---------------------------------------------------------------------------------
- // Insert the second string before the first string.
-
- StringPtr
- PrefixPStr( Str255 ioFirstStr, ConstStr255Param inSecondStr )
- {
- // Limit the combined string to 255 chars.
- Size theNumChars = *inSecondStr;
- if ( *ioFirstStr + theNumChars > 255 ) {
- theNumChars = 255 - *ioFirstStr;
- }
-
- // Move the first string to make room for the prefix.
- BlockMoveData( ioFirstStr + 1, ioFirstStr + theNumChars + 1, *ioFirstStr );
-
- // Move the second string into the first.
- BlockMoveData( inSecondStr + 1, ioFirstStr + 1, theNumChars );
-
- // Set length of combined string
- *ioFirstStr += theNumChars;
-
- return ioFirstStr;
- }
-